package com.shiguiwu.springmybatis.javabase.nio.channel;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: 通道
 * @author: stone
 * @date: Created by 2021/9/25 11:45
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.nio.channel
 */
public class ChannelTests {


    //根据RandomAccessFile对象获取
    public static void main(String[] args) throws Exception{

        readFile();

        //writeFile();


        //transferFrom();


        //transferTo();

    }


    public static void readFile() throws Exception {
        RandomAccessFile accessFile = new RandomAccessFile("D:\\shiguiwu\\Desktop\\备忘.txt", "rw");

        //获取一个通道
        FileChannel channel = accessFile.getChannel();
        //n创建一个缓冲区

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        //读入缓冲区
        int read = 0;
        while ((read = channel.read(buffer)) != -1) {
            System.out.println("读取了：" + read);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print((char) buffer.get());
            }
            System.out.println("===================>");
            buffer.clear();
        }
        channel.close();
        accessFile.close();
    }

    public static void writeFile() throws Exception {
        RandomAccessFile accessFile = new RandomAccessFile("F:\\tmp\\channel.txt", "rw");

        //获取一个通道
        FileChannel channel = accessFile.getChannel();
        //n创建一个缓冲区

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        //清理一下
        buffer.clear();

        String data = "zhoudongxiu";

        buffer.put(data.getBytes());

        buffer.flip();
        //
        //FileChannel完成最终实现
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }

        channel.close();
        accessFile.close();

    }

    /**
     * 采用的是零拷贝
     * @throws Exception
     */
    public static void transferFrom() throws Exception {
        RandomAccessFile fromFile = new RandomAccessFile("F:\\tmp\\channel.txt", "rw");
        RandomAccessFile toFile = new RandomAccessFile("F:\\tmp\\channel1.txt", "rw");

        FileChannel fromFileChannel = fromFile.getChannel();
        FileChannel toFileChannel = toFile.getChannel();

        long position = 0;
        //fromChannel 传输到 toChannel
        toFileChannel.transferFrom(fromFileChannel, position, fromFileChannel.size());

        fromFile.close();
        toFile.close();

    }

    /**
     * todo 采用的是零拷贝
     * @throws Exception
     */
    public static void transferTo() throws Exception{
        RandomAccessFile fromFile = new RandomAccessFile("F:\\tmp\\channel.txt", "rw");
        RandomAccessFile toFile = new RandomAccessFile("F:\\tmp\\channel2.txt", "rw");

        FileChannel fromFileChannel = fromFile.getChannel();
        FileChannel toFileChannel = toFile.getChannel();

        long position = 0;
        //fromFileChannel 传输到 toChannel
        fromFileChannel.transferTo(position, fromFileChannel.size(), toFileChannel);

        fromFile.close();
        toFile.close();
    }

}
